package com.gamecauca;

import java.util.List;

import android.util.Log;

import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class Animation {
	public static final int ANIMATION_LOOPING = 0;
	public static final int ANIMATION_NONLOOPING = 1;

	final TextureRegion[] keyFrames;
	final float frameDuration;

	public Animation (float frameDuration, TextureRegion... keyFrames) {
		this.frameDuration = frameDuration;
		this.keyFrames = keyFrames;
	}
	/** Constructor, storing the frame duration and key frames.
	 * 
	 * @param frameDuration the time between frames in seconds.
	 * @param keyFrames the {@link TextureRegion}s representing the frames. */
	public Animation (float frameDuration, List keyFrames) {
		this.frameDuration = frameDuration;
		this.keyFrames = new TextureRegion[keyFrames.size()];
		for (int i = 0, n = keyFrames.size(); i < n; i++) {
			this.keyFrames[i] = (TextureRegion)keyFrames.get(i);
		}
	}

	public TextureRegion getKeyFrame (float stateTime, int mode) {
		int frameNumber = (int)(stateTime / frameDuration);

		if (mode == ANIMATION_NONLOOPING) {
			frameNumber = Math.min(keyFrames.length - 1, frameNumber);
		} else {
			frameNumber = frameNumber % keyFrames.length;
		}
		return keyFrames[frameNumber];
		
	}
	/**
	 * Whether the animation would be finished if played without looping
	 * given the state time.
	 * @param stateTime
	 * @return whether the animation is finished.
	 */
	public boolean isAnimationFinished (float stateTime) {
	    int frameNumber = (int)(stateTime / frameDuration);
	    return keyFrames.length - 1 < frameNumber;
	}
}

